package org.teremail;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
import java.util.Collection;

import javax.sql.DataSource;

import org.postgresql.ds.PGSimpleDataSource;
import org.teremail.schema.Table;
import org.teremail.util.DB;

public class DropAll {

    public static void main(String[] args) {
        
        for (DataSource ds : getDatabases()) {
            Connection cn = null;

            try {
                cn = ds.getConnection();
                
                for (Table t : Schema.ALL_TABLES) {
                    String sql = "drop table " + t.getName();
                    System.out.printf("Dropping table %s\n", t.getName());
                    Statement st = null;
                    try {
                        st = cn.createStatement();
                        st.execute(sql);
                    } catch (SQLException e) {
                        System.err.printf("Failed to drop table %s\n", t.getName());
                    } finally {
                        DB.close(st);
                    }
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                DB.close(cn);
            }
        }
    }

    private static Collection<DataSource> getDatabases() {
        PGSimpleDataSource ds = new PGSimpleDataSource();
        ds.setDatabaseName("test");
        ds.setUser("mike");
        ds.setPassword("test");
        ds.setServerName("localhost");
        ds.setPortNumber(5432);
        return Arrays.asList((DataSource) ds);
    }
}
